<?php
/**
 * Doit stocker les futurs objets ajax via une key
 * 
 * 
 */
class Tv_Arte_Helpers_View_JQuery extends Zend_View_Helper_Abstract
{
    
    public $DEBUG               = false;
    const AJAX_SUCCESS          = 'success';
    const AJAX_COMPLETE         = 'complete';
    private $_js                = Array();
    private $_addScriptTags     = false;
    
    public function __construct() {
        $this->DEBUG = defined('APPLICATION_ENV') && APPLICATION_ENV == 'development';
    }
    /**
     * Wrap response with script tags
     * 
     * @param Boolean $bool
     * @return Tv_Arte_Helpers_View_JQuery 
     */
    public function addScriptTags($bool)
    {
        $this->_addScriptTags = $bool;
        return $this;
    }
    /**
     * Open javascript tag
     * 
     * @return String
     */
    public function openScript()
    {
        return '<script type="text/javascript">/*<![CDATA[*/' . (($this->DEBUG) ? PHP_EOL : '');
    }
    /**
     * Close javascript tag
     * 
     * @return String 
     */
    public function closeScript()
    {
        return '/*]]>*/</script>' . (($this->DEBUG) ? PHP_EOL : '');
    }
    /**
     * View Helper getter
     * 
     * @return Tv_Arte_Helpers_View_JQuery 
     */
    public function JQuery()
    {
        return $this;
    }
    /**
     *
     * @param String $url
     * @param Array $settings
     * @return Tv_Arte_Helpers_View_JQuery 
     */
    private $_xhr = null;
    public function ajax($url, $settings=Array())
    {
        $this->_xhr = uniqid('xhr');
        $this->_js[] = sprintf('var %s = jQuery.ajax("%s", %s);', $this->_xhr, $url, $this->_getLiteral($settings)) . (($this->DEBUG) ? PHP_EOL : '');
        return $this;
    }
    /**
     * Object deferred success
     * 
     * @param String $function
     * @return Tv_Arte_Helpers_View_JQuery 
     */
    public function ajaxDone($function)
    {
       $this->_js[] = sprintf('%s.done(%s);', $this->_xhr, $function) . (($this->DEBUG) ? PHP_EOL : '');
       return $this;
    }
    
    
    public function openFunction($functionName) 
    {
        $this->_js[] = sprintf('%s = function () {', $functionName) . (($this->DEBUG) ? PHP_EOL : '');
        return $this;
    }
     public function closeFunction() 
    {
        $this->_js[] = '};' . (($this->DEBUG) ? PHP_EOL : '');
        return $this;
    }
    /**
     * Magic function __toString
     *
     * @return String
     */
    public function __toString() {
        if ($this->_addScriptTags) {
            array_unshift($this->_js, $this->openScript());
            array_push($this->_js, $this->closeScript());
        }
        $stringBuffer = implode('', $this->_js);
        $this->_js = Array();
        return $stringBuffer;
    }
    /**
     * Transforms a simple php assoc array in a javascript literal
     * 
     * @param array $data
     * @return string 
     */
    private function _getLiteral(Array $data)
    {
        $javascriptLiteral = "{" . (($this->DEBUG) ? PHP_EOL : '');
        $lastKey = array_pop(array_keys($data));
        foreach ($data as $k => $v) {
            $javascriptLiteral .= $k . ":" . $v . ((strcmp($lastKey, $k) !== 0) ? ", " : "") . (($this->DEBUG) ? PHP_EOL : '');
        }
        $javascriptLiteral .= "}" . (($this->DEBUG) ? PHP_EOL : '');
        return $javascriptLiteral;
    }
}